﻿using System;
using System.Threading.Tasks;

namespace RuoVea.ExCache.Cache
{
    /// <summary>
    /// 缓存接口
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// 从缓存里取数据，如果不存在则执行查询方法，
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">键值</param>
        /// <param name="GetFun">查询方法</param>
        /// <param name="timeSpanMin">有效期 单位分钟/param>
        /// <returns></returns>
        T Cof_ReadWrite<T>(string key, Func<T> GetFun, TimeSpan timeSpanMin) where T : class;

        #region Del
        /// <summary>
        /// 用于在 key 存在时删除 key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>long</returns>
        long Del(params string[] key);

        /// <summary>
        /// 用于在 key 存在时删除 key
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>long</returns>
        Task<long> DelAsync(params string[] key);

        /// <summary>
        /// 用于在 key 模板存在时删除  删除带该前缀的数据
        /// </summary>
        /// <param name="pattern">pattern为前缀key: pattern* , 含有pattern的key: *pattern*</param>
        /// <returns>bool</returns>
        Task<long> DelByProfixAsync(string pattern);
        #endregion

        #region Exists
        /// <summary>
        /// 检查给定 key 是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>bool</returns>
        bool Exists(string key);

        /// <summary>
        /// 检查给定 key 是否存在
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>bool</returns>
        Task<bool> ExistsAsync(string key);
        #endregion

        #region Read
        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>string</returns>
        string Read(string key);

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <typeparam name="T">byte[] 或其他类型</typeparam>
        /// <param name="key">键</param>
        /// <returns>T</returns>
        T Read<T>(string key);

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <param name="key">键</param>
        /// <returns>string</returns>
        Task<string> ReadAsync(string key);

        /// <summary>
        /// 获取指定 key 的值
        /// </summary>
        /// <typeparam name="T">byte[] 或其他类型</typeparam>
        /// <param name="key">键</param>
        /// <returns>T</returns>
        Task<T> ReadAsync<T>(string key);
        #endregion

        #region Write
        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>bool</returns>
        bool Write(string key, object value);

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expire">有效期</param>
        /// <returns>bool</returns>
        bool Write(string key, object value, TimeSpan expire);

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>bool</returns>
        Task<bool> WriteAsync(string key, object value);

        /// <summary>
        /// 设置指定 key 的值，所有写入参数object都支持string | byte[] | 数值 | 对象
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="expire">有效期</param>
        /// <returns>bool</returns>
        Task<bool> WriteAsync(string key, object value, TimeSpan expire);
        #endregion
    }
}
